package com.gzc.just.play.last.war.centralserver.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Ranking Service
 * Manages player rankings and leaderboards
 */
@Service("central-rankingService")
public class RankingService {
    private static final Logger logger = LoggerFactory.getLogger(RankingService.class);
    
    // Player ranking data (playerId -> rank)
    private final Map<Long, Integer> playerRanks = new ConcurrentHashMap<>();
    
    /**
     * Add player to ranking system
     */
    public void addPlayer(long playerId, String playerName) {
        // Generate rank based on number of players
        int rank = playerRanks.size() + 1;
        playerRanks.put(playerId, rank);
        
        logger.info("Player {} added to ranking system with rank {}", playerName, rank);
    }
    
    /**
     * Get player rank
     */
    public int getPlayerRank(long playerId) {
        return playerRanks.getOrDefault(playerId, 0);
    }
    
    /**
     * Get top players
     */
    public List<Map.Entry<Long, Integer>> getTopPlayers(int count) {
        return playerRanks.entrySet().stream()
                .sorted(Map.Entry.comparingByValue())
                .limit(count)
                .toList();
    }
    
    /**
     * Get player ranking list
     */
    public List<Map<String, Object>> getRankingList(String type, int limit) {
        return playerRanks.entrySet().stream()
                .sorted(Map.Entry.comparingByValue())
                .limit(limit)
                .map(entry -> {
                    Map<String, Object> playerInfo = new HashMap<>();
                    playerInfo.put("playerId", entry.getKey());
                    playerInfo.put("rank", entry.getValue());
                    playerInfo.put("score", entry.getValue() * 100);
                    playerInfo.put("rankingType", type);
                    return playerInfo;
                })
                .toList();
    }
    
    /**
     * Get player rank by ID
     */
    public Map<String, Object> getPlayerRanking(Long playerId, String rankingType) {
        Integer rank = playerRanks.get(playerId);
        Map<String, Object> result = new HashMap<>();
        result.put("playerId", playerId);
        result.put("rankingType", rankingType);
        result.put("rank", rank != null ? rank : -1);
        result.put("score", rank != null ? rank * 100 : 0);
        result.put("isRanked", rank != null);
        return result;
    }
    
    /**
     * Get friend rankings
     */
    public List<Map<String, Object>> getFriendRanking(Long playerId, String type) {
        // Simple friend list implementation
        // In a real system, this would query social relationships
        return playerRanks.entrySet().stream()
                .filter(entry -> isFriend(entry.getKey(), playerId))
                .sorted(Map.Entry.comparingByValue())
                .limit(10)
                .map(entry -> {
                    Map<String, Object> friendInfo = new HashMap<>();
                    friendInfo.put("playerId", entry.getKey());
                    friendInfo.put("rank", entry.getValue());
                    friendInfo.put("score", entry.getValue() * 100);
                    friendInfo.put("isFriend", true);
                    return friendInfo;
                })
                .toList();
    }
    
    /**
     * Check if two players are friends
     */
    private boolean isFriend(Long player1Id, Long player2Id) {
        // Simple implementation - would check social data in real system
        return Math.abs(player1Id - player2Id) <= 100; // Example heuristic
    }
}